package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.Course;
import cn.itsource.ymcc.domain.CourseChapter;
import cn.itsource.ymcc.mapper.CourseChapterMapper;
import cn.itsource.ymcc.service.ICourseChapterService;
import cn.itsource.ymcc.service.ICourseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;
import java.util.List;

/**
 * <p>
 * 课程章节 ， 一个课程，多个章节，一个章节，多个视频 服务实现类
 * </p>
 *
 * @author whale
 * @since 2021-09-12
 */
@Service
@Transactional
public class CourseChapterServiceImpl extends ServiceImpl<CourseChapterMapper, CourseChapter> implements ICourseChapterService {

    @Autowired
    private ICourseService courseService;

    @Autowired
    private CourseChapterMapper courseChapterMapper;

    @Override
    public boolean insert(CourseChapter entity) {

        Course course = courseService.selectById(entity.getCourseId());
        course.setChapterCount(course.getChapterCount()+1);
        courseService.updateById(course);

        //查询最大序号
        int maxNumber = courseChapterMapper.selectMaxNumberByCourseId(entity.getCourseId());

        entity.setNumber(maxNumber+1);
        return super.insert(entity);
    }

    @Override
    public List<CourseChapter> listByCourseId(Long courseId) {
        return courseChapterMapper.listByCourseId(courseId) ;
    }
}
